package com.cauli.manage.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cauli.manage.user.model.dto.UserAddDTO;
import com.cauli.manage.user.model.vo.UserDetailVO;
import com.cauli.manage.user.model.dto.UserQueryDTO;
import com.cauli.manage.user.model.dto.UserUpdateDTO;
import com.cauli.manage.user.model.dto.UserUpdatePwdDTO;
import com.cauli.manage.user.model.vo.UserUpdateStatusDTO;
import com.cauli.manage.user.model.entity.User;
import com.cauli.manage.user.service.UserService;
import com.cauli.utils.util.ResultResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.qiwenshare.common.anno.MyLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Cauli
 * @date 2022/12/15 15:08
 * @description 用户 前端控制器
 */
@Api(tags = "用户")
@ApiSort(9)
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    public static final String CURRENT_MODULE = "用户";

    @ApiOperationSupport(order = 1)
    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    public ResultResponse<User> listUser(UserQueryDTO userQueryDTO) {
        Page<User> userPage = userService.getUserPage(userQueryDTO);
        return ResultResponse.success().dataList(userPage.getRecords(), userPage.getTotal());
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("根据用户ID获取详细信息")
    @GetMapping({"/", "/{userId}"})
    public ResultResponse<UserDetailVO> getUserDetail(@PathVariable(value = "userId", required = false) Long userId) {
        UserDetailVO userDetailVO = userService.getUserDetail(userId);
        return ResultResponse.success().data(userDetailVO);
    }

    @MyLog(operation = "新增用户", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 3)
    @ApiOperation("新增用户")
    @PostMapping
    public ResultResponse<Void> addUser(@Valid @RequestBody UserAddDTO userAddDTO) {
        userService.addUser(userAddDTO);
        return ResultResponse.success().message("添加成功");
    }

    @MyLog(operation = "修改用户", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 4)
    @ApiOperation("修改用户")
    @PutMapping
    public ResultResponse<Void> updateUser(@RequestBody UserUpdateDTO userUpdateDTO) {
        userService.updateUser(userUpdateDTO);
        return ResultResponse.success().message("修改成功");
    }

    @MyLog(operation = "用户状态修改", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 5)
    @ApiOperation("用户状态修改")
    @PutMapping("/status")
    public ResultResponse<Void> changeUserStatus(@Valid @RequestBody UserUpdateStatusDTO userUpdateStatusDTO) {
        userService.changeUserStatus(userUpdateStatusDTO);
        return ResultResponse.success().message("修改成功");
    }

    @MyLog(operation = "修改密码", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 6)
    @ApiOperation("修改密码")
    @PutMapping("/password")
    public ResultResponse<Void> resetUserPassword(@Valid @RequestBody UserUpdatePwdDTO userUpdatePwdDTO) {
        userService.resetUserPassword(userUpdatePwdDTO);
        return ResultResponse.success().message("密码修改成功");
    }

    @MyLog(operation = "删除用户", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 7)
    @ApiOperation("删除用户")
    @DeleteMapping("/{userIds}")
    public ResultResponse<Void> deleteUser(@PathVariable List<Long> userIds) {
        userService.deleteUsers(userIds);
        return ResultResponse.success().message("删除成功");
    }
}
